//
//  SHKViewController.m
//  YsSmartPenModule
//
//  Created by John01 on 04/22/2020.
//  Copyright (c) 2020 John01. All rights reserved.
//

#import "SHKViewController.h"
#import <YsSmartPenCoreModule/YsSmartPenCoreModule.h>

@interface SHKViewController ()
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSMutableArray <Dot *> *> *dict;
@property(nonatomic, strong) NSMutableArray <NSMutableArray <Dot *> *>*dotsArray;
@property(nonatomic, strong) NSMutableArray <NSString *> *numsArray;
@end

@implementation SHKViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    [self didInitializeWithStyle:UITableViewStyleGrouped];
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"songhongkang" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:jsonPath];

    [[array qmui_mapWithBlock:^id(id item) {
        return [Dot modelWithJSON:item];
    }] enumerateObjectsUsingBlock:^(Dot *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableArray <Dot *> *array = [self.dict objectForKey:[NSString qmui_stringWithNSInteger:obj.PageID]];
        NSLog(@"%ld",obj.timelong);
        if (!array) {
            array = [NSMutableArray array];
            [self.dict setValue:array forKey:[NSString qmui_stringWithNSInteger:obj.PageID]];
        }
        [array appendObject:obj];
    }];

    [self.dict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSMutableArray<Dot *> * _Nonnull obj, BOOL * _Nonnull stop) {
       [self.dotsArray appendObject:obj];
       [self.numsArray appendObject:key];
    }];
}

- (void)initTableView
{
    [super initTableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dict.allKeys.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenWidth * 1512/1075;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     NSString *identifier = [NSString stringWithFormat:@"%zd",indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    CGRect rect = CGRectMake(0, 0, kScreenWidth, kScreenWidth * 1512/1075);
    DrawImageView * _drawView =  [[DrawImageView alloc] initWithFrame:rect];
    [_drawView drawStrokeWithArray:self.dotsArray[indexPath.row] withPageId:self.numsArray[indexPath.row]];
    [cell addSubview:_drawView];
    return cell;
}

- (NSMutableDictionary<NSString *,NSMutableArray<Dot *> *> *)dict
{
    if (!_dict) {
          _dict = [NSMutableDictionary dictionary];
      }
      return _dict;
}

- (NSMutableArray<NSMutableArray<Dot *> *> *)dotsArray
{
    if (!_dotsArray) {
        _dotsArray = [NSMutableArray array];
    }
    return _dotsArray;
}

- (NSMutableArray<NSString *> *)numsArray
{
    if (!_numsArray) {
        _numsArray = [NSMutableArray array];
    }
    return _numsArray;
}
@end
